<?php
/**
 * 注意：
 *
 * - 模型所属命名空间必须为 Model。
 * - 模型层中的各方法在接收来自控制层的参数值时，必须验证其是否为期望值，因为很难保证所有控制层单元传过来的参数值一定合法。
 * - 每一个被控制层调用的模型层方法都应该有明确的返回值，控制层也应该对该返回值做出相应的处理。
 *
 */
namespace Model;

use Lib, Dao;

class Member {
	private $DMember = NULL;

	/**
	 * 构造方法，禁止直接 new Model\Member，而必须是 Model\Member::getInstance()
	 *
	 */
	private function __construct() {
		$this->DMember = Dao\Member::getInstance();
	}

	/**
	 * 注册一个新用户
	 *
	 * @param string $username
	 * @param string $password
	 * @param string $name
	 * @param string $email
	 * @return boolean
	 */
	public function sigin($username, $password, $name, $email) {
		$member = array();
		$member['username'] = $username;
		$member['password'] = $password;
		$member['name'] = $name;
		$member['email'] = $email;
		$member['marktime'] = date('Y-m-d H:i:s');
		
		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
			return FALSE;
		}
		
		$id = $this->DMember->insert($member);
		if ($id <= 0) {
			return FALSE;
		}
		
		$id = $this->DMember->insert((object) $member);
		if ($id <= 0) {
			return FALSE;
		}
		
		$id = $this->DMember->insert($member, array_keys($member));
		if ($id <= 0) {
			return FALSE;
		}
		
		$id = $this->DMember->insert(array($member, $member), array_keys($member));
		if ($id <= 0) {
			return FALSE;
		}
		
		return TRUE;
	}

	public function member($page) {
		$Pager = new Lib\Pager();
		
		$Pager->set(Lib\Pager::SET_PAGE_SIZE, 5);
		$Pager->set(Lib\Pager::SET_GROUP_SIZE, 5);
		$Pager->set(Lib\Pager::SET_FETCH_PAGE, $page);
		
		$Pager->handle(Dao\AppsDb::getInstance());
		
		$Pager->select($this->DMember->prepare(NULL, 'id DESC'));
		
		return $Pager;
	}

	public static function getInstance() {
		static $instance = NULL;
		
		if (!$instance) {
			$instance = new self();
		}
		
		return $instance;
	}
}